package com.ycsj.goldmedalnewconcept.teacher.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.view.scroll.MyListView;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.activity.SingleQuestionDetailsActivity;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.teacher.activity.TeacherAddQuestionsActivity;
import com.ycsj.goldmedalnewconcept.teacher.utils.TextUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 题目列表的适配器
 * Created by Administrator on 2019/2/21 0021.
 */

public class QuestionRvAdapter extends BaseQuickAdapter<TkItemBean, BaseViewHolder> {

    private Context context;
    private TeacherAddQuestionsActivity activity;

    public QuestionRvAdapter(TeacherAddQuestionsActivity activity, Context context, int layoutResId, @Nullable List<TkItemBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.activity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, final TkItemBean item) {

        //是否选中
        ImageView lvChoose = helper.getView(R.id.lv_choose);
        if (item.isChoosed()) {
            lvChoose.setImageResource(R.drawable.checked);
        } else {
            lvChoose.setImageResource(R.drawable.unchecked);
        }

        //包含选中按钮的线性布局
        LinearLayout llChoose = helper.getView(R.id.ll_choose);
        //点击事件
        llChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!item.isChoosed()) {
                    //选中时
                    if (activity.getQuestionNum() <= 0) {
                        //添加的题目数量>可添加的题目总数时,不能继续添加
                        ToastUtils.showShort("练习题目数量已达到20道,不能继续添加");
                    } else {
                        //将选择的问题添加到集合中保存
                        activity.getAddQuestionInfo().add(item);
                        //更新可选择的最大题目数量
                        activity.setQuestionNum(activity.getQuestionNum() - 1);
                        item.setChoosed(!item.isChoosed());
                        notifyDataSetChanged();

                        BusInfo businfo = new BusInfo();
                        businfo.type = TeacherAddQuestionsActivity.NUMBER;
                        EventBus.getDefault().post(businfo);

                    }
                } else {
                    //取消时
                    //将该题目从集合中移除
                    activity.getAddQuestionInfo().remove(item);
                    //同时更新可选择的最大题目数量
                    activity.setQuestionNum(activity.getQuestionNum() + 1);
                    item.setChoosed(!item.isChoosed());
                    notifyDataSetChanged();

                    BusInfo businfo = new BusInfo();
                    businfo.type = TeacherAddQuestionsActivity.NUMBER;
                    EventBus.getDefault().post(businfo);
                }
            }
        });

        //题目类型和题干,题目类型需要进行变色
        TextView tv = helper.getView(R.id.tv_question);
        //拼接题目类型和题干
        String tvConent;
        if (TextUtils.isEmpty(item.getN_topic_name())) {
            tvConent = "(" + item.getN_type_name() + ")" + item.getContent();
        } else {
            tvConent = "(" + item.getN_type_name() + "-" + item.getN_topic_name() + ")" + item.getContent();
        }


        if ("6".equals(item.getTypeid())) {

            for (int i = 0; i < item.getSub_list().size(); i++) {
                if (i == 0) {
                    tvConent += "\n" + (i + 1) + "." + item.getSub_list().get(i).getTitle() + "\n";
                } else {
                    tvConent += (i + 1) + "." + item.getSub_list().get(i).getTitle() + "\n";
                }
            }
        }

        tv.setText(tvConent);
        //对题目类型做变色处理
        if (TextUtils.isEmpty(item.getN_topic_name())) {
            tv.setText(TextUtil.setColor(context, tvConent, "(" + item.getN_type_name() + ")", R.color.change_questiontype_color));
        } else {
            tv.setText(TextUtil.setColor(context, tvConent, "(" + item.getN_type_name() + "-" + item.getN_topic_name() + ")", R.color.change_questiontype_color));

        }
        //题干的点击事件,跳转到题目详情的页面
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 跳转到每个题型的详细界面,查看单题
                SingleQuestionDetailsActivity.goActivity(context, item, 3 + "");
            }
        });

        //答案选项的ListView
        MyListView lv = helper.getView(R.id.lv);

        lv.setVisibility(View.GONE);

    }

    /**
     * 判断集合里面是否全是空串的方法
     *
     * @param contents
     * @return
     */
    public boolean isEmpty(List<String> contents) {
        boolean res = true;
        for (int i = 0; i < contents.size(); i++) {
            if (!TextUtils.isEmpty(contents.get(i))) {
                res = false;
            }
        }

        return res;
    }

}
